package org.luxor.commons.storage.client.aliyun;

import org.luxor.commons.core.utils.StrKit;
import org.luxor.commons.core.utils.StringUtils;
import org.luxor.commons.storage.client.AbstractStorageProperties;
import org.luxor.commons.storage.config.HttpConfig;
import org.luxor.commons.storage.config.ThumbImageConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

/**
 * Aliyun-Client 配置
 *
 * @author Mr.yan @date 2020/2/8
 */
@ConfigurationProperties(prefix = AliyunProperties.PREFIX)
public class AliyunProperties extends AbstractStorageProperties {
    public static final String PREFIX = "spring.storage.aliyun";

    final Boolean DEFAULT_ENABLED = Boolean.FALSE;
    final Integer DEFAULT_CHUNK_SIZE = 5;
    final Integer DEFAULT_DOWNLOAD_CHUNK_SIZE = 100;
    final HttpConfig DEFAULT_HTTP = new HttpConfig();
    final ThumbImageConfig DEFAULT_THUMB_IMAGE = new ThumbImageConfig();

    final String DEFAULT_ACCESS_KEY_ID = "none";
    final String DEFAULT_ACCESS_KEY_SECRET = "none";
    final String DEFAULT_END_POINT = "none";
    final String DEFAULT_BUCKET_NAME = "none";
    final String DEFAULT_DOMAIN = "https://xxxx.aliyun.com";
    final String DEFAULT_PREFIX = "default";

    /**
     * 是否启用
     */
    private boolean enabled;

    /**
     * 分块大小
     */
    private Integer chunkSize;
    private Integer downloadChunkSize;

    /**
     * 缩略图配置
     */
    @NestedConfigurationProperty
    private ThumbImageConfig thumbImage;

    /**
     * 防盗链配置
     */
    @NestedConfigurationProperty
    private HttpConfig http;


    /**
     * 阿里云ACCESS_KEY_ID
     */
    private String accessKeyId;

    /**
     * 阿里云ACCESS_KEY_SECRET
     */
    private String accessKeySecret;

    /**
     * 阿里云EndPoint
     */
    private String endPoint;

    /**
     * 阿里云存储空间名
     */
    private String bucketName;

    /**
     * 阿里云文件路径前缀
     */
    private String prefix;

    /**
     * 阿里云外链域名
     */
    private String domain;


    public AliyunProperties() {
        super();
        this.enabled = DEFAULT_ENABLED.booleanValue();
        this.chunkSize = DEFAULT_CHUNK_SIZE;
        this.downloadChunkSize = DEFAULT_DOWNLOAD_CHUNK_SIZE;
        this.thumbImage = DEFAULT_THUMB_IMAGE;
        this.http = DEFAULT_HTTP;

        this.accessKeyId = DEFAULT_ACCESS_KEY_ID;
        this.accessKeySecret = DEFAULT_ACCESS_KEY_SECRET;
        this.endPoint = DEFAULT_END_POINT;
        this.bucketName = DEFAULT_BUCKET_NAME;
        this.prefix = DEFAULT_PREFIX;
        this.domain = DEFAULT_DOMAIN;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public Integer getChunkSize() {
        return chunkSize;
    }

    public void setChunkSize(Integer chunkSize) {
        this.chunkSize = chunkSize;
    }

    public Integer getDownloadChunkSize() {
        return downloadChunkSize;
    }

    public void setDownloadChunkSize(Integer downloadChunkSize) {
        this.downloadChunkSize = downloadChunkSize;
    }

    @Override
    public ThumbImageConfig getThumbImage() {
        return thumbImage;
    }

    public void setThumbImage(ThumbImageConfig thumbImage) {
        this.thumbImage = thumbImage;
    }

    @Override
    public HttpConfig getHttp() {
        return http;
    }

    public void setHttp(HttpConfig http) {
        this.http = http;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }

    public String getEndPoint() {
        return endPoint;
    }

    public void setEndPoint(String endPoint) {
        this.endPoint = endPoint;
    }

    public String getBucketName() {
        return bucketName;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }

    public String getPrefix() {
        if (StringUtils.endsWith(prefix, StrKit.SLASH)) {
            return prefix.substring(0, prefix.length() - 1);
        } else {
            return prefix;
        }
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        if (StringUtils.endsWith(domain, StrKit.SLASH)) {
            this.domain = domain.substring(0, domain.length() - 1);
        } else {
            this.domain = domain;
        }
    }
}
